/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package car_1;

import com.vividsolutions.jts.geom.Point;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author marcin
 * idgps jest losowany, najlepiej ustawić stałe idgps,
 * stałe idgps oznacza jeden samochód,
 * aplikacja PZMD-application losoqała idgps, ze względu na to że
 * była uruchomiona jak wątek
 *
 */
public class Main {

    @Resource(mappedName = "jms/ConnectionFactory")
    private static ConnectionFactory connectionFactory;
    @Resource(mappedName = "jms/Queue")
    private static Queue queue;
    
    private static long idGps = 0;
    private static int numberSteeet;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws JMSException, InterruptedException {

         EntityManagerFactory emf = Persistence.createEntityManagerFactory("Car_1PU");
     EntityManager em = emf.createEntityManager();
        Connection connection = connectionFactory.createConnection();



        Session session = null;
        MessageProducer messageProducer = null;
        TextMessage message = null;

        try {

            //  connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.SESSION_TRANSACTED);

            messageProducer = session.createProducer(queue);
            message = session.createTextMessage();

            Random random = new Random();
            Float x = random.nextFloat(); //losowanie poĹ‚oĹĽenia samochodu wsp.x
            Float y = random.nextFloat();//losowanie poĹ‚oĹĽenia samochodu wsp. y
            Calendar date = new GregorianCalendar();
            idGps = random.nextInt(100000);
            Float l; //dĂłĹ‚ [0,1]
            Float h; //gĂłra [0,1] gĂłra musi byÄ‡ wiÄ™ksza od doĹ‚u

            City c = new City();
            boolean isCheangeStreet = false;
            int count = 0;

            while (true) {
                System.out.println(numberSteeet + "to jest numer ulicy");
                x = random.nextFloat();
                l = x;
                h = new Float(1);
                Point p = new City().randomPoint(numberSteeet, l, h, em);
                @SuppressWarnings("static-access")

                int hour = date.get(date.HOUR);
                if (date.get(date.AM_PM) == 1) {
                    hour += 12;
                }
                String msg = idGps + " " + p.getX() + " " + p.getY() + " "
                        + date.get(date.YEAR) + " " + date.get(date.MONTH) + " " + date.get(date.DAY_OF_MONTH) + " " + hour + " " + date.get(date.MINUTE) + " " + date.get(date.SECOND);
                System.out.println(msg);
                message.setText(msg);

                messageProducer.send(message);
                TimeUnit.SECONDS.sleep(5);
                if (count >= 5) {
                    numberSteeet = random.nextInt(6);
                    numberSteeet = 5;
                    count = 0;
                } else {
                    count++;
                }



            }

        }  catch (JMSException e) {
            System.out.println("blad: " + e.toString());
        } finally {
            if (connection != null) {

                try {
                    connection.close();
                } catch (JMSException e) {
                    System.out.println("JMSException");
                }
            } // if

           


        }
    }
}
